package com.jnu.myllm.ui.intercepter;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginIntercepter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("登录拦截器");
        // 刺探请求直接放行（没有jwt凭证所以会被拦截）
        if(request.getMethod().equals("OPTIONS")){
            return true;
        }
        if (request.getAttribute("user") != null)
            return true;
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().print("{\"code\":4001,\"msg\":\"未登录\",\"data\":null}");
        return false;
    }
}
